package com.sita.linboard.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.sita.linboard.base.BaseApplication;
import com.sita.linboard.driverInfo.CommonRestBean;
import com.sita.linboard.driverInfo.UpdateDriverLocationRequest;
import com.sita.linboard.http.RestClient;
import com.sita.linboard.utils.SpUtils;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class LocationService extends Service implements AMapLocationListener {
    private static final String TAG = "LocationService";
    //声明mLocationOption对象
    public AMapLocationClientOption mLocationOption = null;
    private IBinder binder = new LocationService.LocationBinder();
    private AMapLocationClient mLocationClient = null;

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mLocationClient = new AMapLocationClient(this);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位监听
        mLocationClient.setLocationListener(this);
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        //设置定位间隔,单位毫秒,默认为2000ms,设置为5分钟
        mLocationOption.setInterval(300000);
        //设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后，在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
        //启动定位
        mLocationClient.startLocation();
        Log.e(TAG, "服务创建");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLocationClient.stopLocation();
        Log.e(TAG, "服务销毁");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                //定位成功回调信息，设置相关消息
                aMapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                aMapLocation.getLatitude();//获取纬度
                aMapLocation.getLongitude();//获取经度
                aMapLocation.getAccuracy();//获取精度信息
                UpdateDriverLocationRequest request = new UpdateDriverLocationRequest();
                request.driverId = SpUtils.getString("AccoundID", null, BaseApplication.getContext());
                request.lat = aMapLocation.getLatitude();
                request.lng = aMapLocation.getLongitude();
                RestClient.getRestService().updateDriverLocation(request, new Callback<CommonRestBean>() {
                    @Override
                    public void success(CommonRestBean commonRestBean, Response response) {
                        Log.e(TAG,"定位更新");
                    }

                    @Override
                    public void failure(RetrofitError error) {
                    }
                });
            } else {
                //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
            }
        }

    }


    /*定义内容类，继承Binder*/
    public class LocationBinder extends Binder {
        LocationService getService() {
            return LocationService.this;
        }
    }
}
